package com.wish.g01.business.controller;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

import com.wish.g01.base.dto.RegisterForm;
import com.wish.g01.base.entity.SysRegion;
import com.wish.g01.base.entity.SysUser;
import com.wish.g01.base.entity.TbBusiness;
import com.wish.g01.base.service.ISysRegionService;
import com.wish.g01.base.service.ISysUserService;
import com.wish.g01.base.service.ITbBusinessService;
import com.wish.g01.common.core.domain.R;
import com.wish.g01.common.core.utils.StringUtils;
import com.wish.g01.common.redis.service.RedisService;
import com.wish.g01.system.api.RemoteSmsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wish.g01.common.core.web.controller.BaseController;
import com.wish.g01.common.core.web.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 商户Controller
 * 
 * @author wish
 * @date 2022-05-25
 */
@RestController
@RequestMapping("/business")
public class BusinessController extends BaseController
{
    @Autowired
    private ITbBusinessService iTbBusinessService;

    @Autowired
    ISysUserService iSysUserService;

    /**
     * 查询商户列表
     */
//    @PreAuthorize(hasPermi = "system:business:list")
      @GetMapping("list")
    public TableDataInfo list(TbBusiness tbBusiness)
    {
        startPage();
        List<TbBusiness> list = iTbBusinessService.list();
        return getDataTable(list);
    }

    // 注入短信远程服务接口


    @Resource
    RemoteSmsService remoteSmsService;

    @Autowired
    RedisService redisService;

    /**
     *  发送验证码
     * @param registerForm
     * @return
     */
    @PostMapping("getCode")
    public R getCode(@RequestBody RegisterForm registerForm){
        String token = registerForm.getToken();
       if (StringUtils.isEmpty(token)){
           return R.fail("token  不能为空");
       }
        // 防止重复发送
        boolean b = redisService.setFormCache(token);
        if (!b){
            return R.fail("重复发送");
        }
        // 发送短信
        String code= (String) remoteSmsService.sendCode(registerForm.getMobile()).getData();
        // 存储redis
        redisService.setCacheObject("sms:"+token,code);
        return R.ok();
    }

    @PostMapping("/add")
    //@RequiresPermissions("premises:premisesinfo:save")
    public R save(@RequestBody RegisterForm registerForm) {
        // 判断 验证码
        String code = redisService.getCacheObject("sms:" + registerForm.getToken());
        if (code == registerForm.getCode()){
            return R.fail("验证码不正确");
        }
        // 判断 密码
        String password = registerForm.getPassword();
        String password2 = registerForm.getPassword2();
        if (!password2.equals(password)){
            return R.fail("密码不统一");
        }
        // 添加用户
        // 添加系统用户表
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(registerForm,sysUser);
        iSysUserService.save(sysUser);
          // 添加 商户表
        TbBusiness business = new TbBusiness();
        // 未审核
        business.setStatus(0);
        business.setUserId(Integer.valueOf(sysUser.getUserId() + ""));
        logger.info("注册用户成功");
        return R.ok(business);
    }

    @PostMapping("/addBusiness")
    //@RequiresPermissions("premises:premisesinfo:save")
    public R addBusiness(@RequestBody TbBusiness business) {
        iTbBusinessService.save(business);
        logger.info("注册用户成功");
        return R.ok();
    }

    @Autowired
    ISysRegionService regionService;

    @GetMapping("getAllCitys")
    public R getAllCitys(){
        // 查询所有地址
        List<SysRegion> regionList = regionService.list();
        // 过滤数据 第一层
        List<SysRegion> regionList1 = regionList.stream().filter(city -> city.getParentId().equals(1)).collect(Collectors.toList());
        for (SysRegion sysRegion : regionList1) {
            // 过滤第二层每个 子类下的 城市
            List<SysRegion> regionList2 = regionList.stream().filter(city -> city.getParentId().equals(sysRegion.getRegionId())).collect(Collectors.toList());
            sysRegion.setChildren(regionList2);
            for (SysRegion region : regionList2) {
                // 过滤第三层每个 子类下的 城市
                List<SysRegion> regionList3 = regionList.stream().filter(city -> city.getParentId().equals(region.getRegionId())).collect(Collectors.toList());
                region.setChildren(regionList3);
            }
        }
        return R.ok(regionList1);
    }

    @PostMapping("/update")
    //@RequiresPermissions("premises:premisesinfo:save")
    public  R update(@RequestBody Integer[] ids) {
        HashMap<Object, Object> objectObjectHashMap = new HashMap<>(17);
        if (ids.length <= 0){
            return R.fail("至少选择一项");
        }
        for (Integer id : ids) {
            // 修改为 已审核
            TbBusiness business = iTbBusinessService.getById(id);
            iTbBusinessService.saveOrUpdate(business.setStatus(3));
        }
        return R.ok();
    }


}
